package com.leoman.service.impl;

import com.leoman.core.Constant;
import com.leoman.dao.TypeDao;
import com.leoman.entity.Type;
import com.leoman.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 2016/3/8.
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeDao typeDao;

    @Override
    public List<Type> findAll() {
        return typeDao.findList();
    }

    @Override
    public Page<Type> find(Integer pageNum, Integer pageSize) {
        return typeDao.findList(new PageRequest(pageNum - 1, pageSize));
    }

    @Override
    public Page<Type> find(Integer pageNum) {
        return typeDao.findList(new PageRequest(pageNum - 1, Constant.PAGE_DEF_SIZE));
    }

    @Override
    public Type getById(Integer id) {
        return typeDao.findOne(id);
    }

    @Override
    public Type deleteById(Integer id) {
        Type type = typeDao.findOne(id);
        type.setIsCut(Constant.IS_CUT_YES);
        typeDao.save(type);
        return null;
    }

    @Override
    public Type create(Type type) {
        return typeDao.save(type);
    }

    @Override
    public Type update(Type type) {
        return typeDao.save(type);
    }

    @Override
    @Transactional
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }

    @Override
    public List<Type> findList() {
        return typeDao.findList();
    }

    @Override
    public Page<Type> findList(Integer pageNum, Integer pageSize) {
        return typeDao.findList(new PageRequest(pageNum - 1, pageSize));
    }

    @Override
    public List<Type> findListName(String name) {
        return typeDao.findListByName(name);
    }
}
